package com.cadre.common.utils.oldutil;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: JianLiStringUtils.class */
public class JianLiStringUtils {
    private static final Pattern DATEPATTERN = Pattern.compile("(\\d{4}\\.\\d{2})$");
    private static final Pattern PATTERN = Pattern.compile("(\\d|\\.)+$");
    private static final String REGEX = "[，。；！？）,.;!?)]";

    public static String byteSubString(String s, int length) {
        int length2 = length * 2;
        byte[] bytes = null;
        try {
            bytes = s.getBytes("Unicode");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int n = 0;
        int i = 2;
        while (i < bytes.length && n < length2) {
            if (i % 2 == 1) {
                n++;
            } else if (bytes[i] != 0) {
                n++;
            }
            i++;
        }
        if (i % 2 == 1) {
            if (bytes[i - 1] != 0) {
                i--;
            } else {
                i++;
            }
        }
        String value = "";
        try {
            value = new String(bytes, 0, i, "Unicode");
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
        }
        return value;
    }

    public static String dateProcessing(String value) {
        Matcher matcher = PATTERN.matcher(value);
        if (DATEPATTERN.matcher(value).find()) {
            return value;
        }
        if (matcher.find()) {
            value = matcher.replaceAll("");
        }
        return value;
    }

    public static String bSubstring(String s, int length) {
        String finalString;
        String string = byteSubString(s, (length * 2) + 2);
        System.out.println(">>>" + string);
        if (string.substring(string.length() - 1).matches(REGEX)) {
            finalString = string.substring(0, string.length() - 1);
        } else {
            finalString = string;
        }
        return dateProcessing(finalString);
    }

    public static int getWordCount(String s) {
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            int ascii = Character.codePointAt(s, i);
            if (ascii < 0 || ascii > 255) {
                length += 2;
            } else {
                length++;
            }
        }
        return length;
    }

    public static String rtrim(String value) {
        if (value == null) {
            return null;
        }
        return value.replaceAll("\\s+$", "");
    }

    public static void main(String[] args) {
        System.out.println(bSubstring("2016.10--2017.07  山西出版传媒集团有限责任公司办事员", 29));
    }
}
